package ejb;

import db.facadelocals.GPFacadeLocal;
import db.facadelocals.PatientFacadeLocal;
import details.GPDetails;
import details.PatientDetails;
import exceptions.GPNotFoundException;
import exceptions.PatientNotFoundException;
import exceptions.UsernameAlreadyExistsException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 * @author jeroen
 */
@Stateless
public class GPBean implements GPLocal {

    @EJB
    private GPFacadeLocal gpFacade;

    @EJB
    private PatientFacadeLocal patientFacade;

    public void registerGP(String username, String password) throws UsernameAlreadyExistsException {
        gpFacade.registerGP(username, password);
    }

    public void registerGP(String username, String password, String name, String surname) throws UsernameAlreadyExistsException {
        gpFacade.registerGP(username, password, name, surname);
    }

    public GPDetails getGP(String username) throws GPNotFoundException{
        return gpFacade.getGP(username);
    }

    public void setGPPassword(String username, String password) throws GPNotFoundException {
        gpFacade.setPassword(username, password);
    }

    public void setGPName(String username, String name) throws GPNotFoundException {
        gpFacade.setName(username, name);
    }

    public void setGPSurname(String username, String surname) throws GPNotFoundException {
        gpFacade.setSurName(username, surname);
    }

    public List<PatientDetails> getAllPatientsForGP(String usernameGP) throws GPNotFoundException {
        List<PatientDetails> pds = new ArrayList<PatientDetails>();

        for(String s : gpFacade.getPatientsFromGP(usernameGP)){
            try {
                pds.add(patientFacade.getPatient(s));
            } catch (PatientNotFoundException ex) {
                //cannot happen
            }
        }

        return pds;
    }

    public void addPatientToGP(String usernameGP, String usernamePatient) throws GPNotFoundException, PatientNotFoundException {
        if(patientFacade.existPatient(usernamePatient)){
            gpFacade.addPatientToGP(usernameGP, usernamePatient);
        } else {
            throw new PatientNotFoundException(usernamePatient);
        }
    }

    public void removePatientFromGP(String usernameGP, String usernamePatient) throws GPNotFoundException, PatientNotFoundException {
        if(patientFacade.existPatient(usernamePatient)){
            gpFacade.removePatientFromGP(usernameGP, usernamePatient);
        } else {
            throw new PatientNotFoundException(usernamePatient);
        }
    }

    public List<GPDetails> getAllGPs() {
        return gpFacade.getAllGPs();
    }
    
    
 
}
